<template>
  <managerNavBar></managerNavBar>
  <van-address-list
    v-model="chosenAddressId"
    :list="listData"
    switchable
    default-tag-text="默认"
    @add="onAdd"
    @edit="onEdit"
  />
</template>

<script setup>
import { ref } from "vue";
import router from "@/router";
import request from "../utils/request";
import managerNavBar from "@/components/managerNavBar.vue";
const userId = localStorage.getItem("userId");
const chosenAddressId = ref("1");
const listData = ref([]);
// 获取所有地址
const getAdr = async () => {
  const list = await request.get("/api/v1/address/" + userId);
  // 遍历所有地址，把每一个地址都添加到listData中：element是每一项地址
  list.data.forEach((element) => {
    listData.value.push({
      id: element.id,
      name: element.real_name,
      tel: element.mobile,
      address: element.city + element.house,
      data: element,
      isDefault: element.is_default? true:false,//判断默认地址
    });
  });
};
getAdr();
const onAdd = async () => {
  router.push("/createadr");
};
// 点击地址跳带着数据转到相应的地址编辑页面
const onEdit = (item) => {
  router.push({ name: "editAdr", params: { data: JSON.stringify(item.data) } });
};
</script>

<style lang="scss" scoped></style>
